package com.edais.actions;

import com.edais.service.GeTuiService;
import com.edais.util.CommonUtil;
import com.edais.util.Consts;
import com.edais.util.Message;
import com.yunzhi.domain.RequestHeader;
import com.yunzhi.domain.ResultCode;
import com.yunzhi.domain.ResultConstant;
import com.yunzhi.domain.ResultDTO;
import com.yunzhi.util.RequestUtil;
import com.yunzhi.util.ResponseUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.HashMap;
/**
 *
 * 此类作废
 * @author admin
 *
 */
@Controller
@RequestMapping("/getui_delete")

public class GeTuiAction implements Serializable {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	private static final Log logger = LogFactory.getLog(GeTuiAction.class);

	@Autowired
	private GeTuiService geTuiService;

	/**
	 * 保存手机设备信息
	 * @param request
	 */
	@RequestMapping(value = "/saveMobileInfo")
	public void saveMobileInfo(HttpServletRequest request, HttpServletResponse response){

		logger.info("----------------------------- /getui/saveMobileInfo  GeTuiAction saveMobileInfo() start ---------------------------");
		try{

			if (!RequestUtil.validSign(request)) {
				logger.info("无效签名");
				ResponseUtil.write(response, ResultConstant.INVALID_SIGN);
				return;
			}
			if (!RequestUtil.validToken(request)) {
				logger.info("无效token");
				ResponseUtil.write(response, ResultConstant.INVALID_TOKEN);
				return;
			}

			// 解密请求参数
			RequestHeader header = RequestUtil.decodeHeader(request);
			logger.info("header：" + header.toString());
			JSONObject params = RequestUtil.decodeDataJson(request);
			logger.info("data：" + params);

			//设备编号
			if(StringUtils.isBlank(params.getString("cid"))){
				/**
				 * 暂时更改为Consts.SUCCESS_CODE,解决前端提示CID不能为空!
				 */
				ResponseUtil.write(response,new ResultDTO(ResultCode.SUCCESS,"CID不能为空!"));
				return;
			}
			if(StringUtils.isBlank(params.getString("type"))){
				/**
				 * 暂时更改为Consts.SUCCESS_CODE,解决前端提示出错!
				 */
				ResponseUtil.write(response,new ResultDTO(ResultCode.SUCCESS,"type不能为空!"));
				return;
			}
			//设备类型
			if("android".equals(params.getString("type").toLowerCase())){
				params.put("deviceType", "2");
			}else if("ios".equals(params.getString("type").toLowerCase())){
				params.put("deviceType", "1");
			}
			ResultDTO result = geTuiService.saveMobileInfo(params);
			ResponseUtil.write(response,result);
		} catch (Exception e) {
			logger.error("----------------------------- /getui/saveMobileInfo GeTuiAction saveMobileInfo() 出现异常!",e);
			ResponseUtil.write(response,ResultConstant.FAILED);
		}
		logger.info("----------------------------- /getui/saveMobileInfo  GeTuiAction saveMobileInfo() end ---------------------------");
		
	}	
	
}
